package com.wl.dzt.sys.core.dao.select;

import com.wl.dzt.sys.util.StringUtil;

import java.util.*;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 查询源信息
 * 用于存放各种数据库查询源信息
 */
public class SelectMate {
    /**
     * 参数
     */
    private List<Object> args = new ArrayList<>();

    /**
     * 各种sql
     */
    private String orderSql = "", whereSql = "", refSql = "", fieldSql = "", pageSql = "";

    /**
     * 所有需要用到的字段
     */
    private Set<String> allFields = new LinkedHashSet<>();

    /**
     * 引用表编号key
     */
    private Set<String> refIds = new HashSet<>();

    /**
     * 主表
     */
    private String baseTable;

    public void addRefId(String id) {
        this.refIds.add(id);
    }

    public List<Object> getArgs() {
        return args;
    }

    public void setArgs(List<Object> args) {
        this.args = args;
    }

    public String getOrderSql() {
        return orderSql;
    }

    public void setOrderSql(String orderSql) {
        this.orderSql = orderSql;
    }

    public String getWhereSql() {
        return whereSql;
    }

    public void setWhereSql(String whereSql) {
        this.whereSql = whereSql;
    }

    public String getRefSql() {
        return refSql;
    }

    public void setRefSql(String refSql) {
        this.refSql = refSql;
    }

    public String getFieldSql() {
        return fieldSql;
    }

    public void setFieldSql(String fieldSql) {
        this.fieldSql = fieldSql;
    }

    public String getPageSql() {
        return pageSql;
    }

    public void setPageSql(String pageSql) {
        this.pageSql = pageSql;
    }

    public Set<String> getAllFields() {
        return allFields;
    }

    public void setAllFields(Set<String> allFields) {
        this.allFields = allFields;
    }

    public Set<String> getRefIds() {
        return refIds;
    }

    public void setRefIds(Set<String> refIds) {
        this.refIds = refIds;
    }

    public String getBaseTable() {
        return baseTable;
    }

    public void setBaseTable(String baseTable) {
        this.baseTable = baseTable;
    }

    /**
     * <b>转换为普通sql</b>
     * <p>
     * 把解析后的一系列元素转换为sql语句
     * </p>
     *
     * @return
     */
    public String toSql() {
        return String.format(" select %s from %s %s", fieldSql, baseTable, getFullBaseSql());
    }

    /**
     * <b>转换为分页sql</b>
     * <p>
     * 把解析后的对象转换为查询语句
     * </p>
     *
     * @return
     */
    public String toPageSql() {
        return String.format(pageSql, toSql());
    }

    /**
     * <b>转换为分页不要查询sql</b>

     *
     * @return
     */
    public String toPageSqlNotOrder() {
        String sql = String.format(" select %s from %s %s", fieldSql, baseTable, getBaseSql());
        return String.format(pageSql, sql);
    }

    /**
     * <b>转换总数sql</b>
     * <p>
     * 把解析后的对象转换为总数查询语句
     * </p>
     *
     * @return
     */
    public String toCountSql() {
        return String.format(" select count(0)  as c from %s %s", baseTable, getBaseSql());
    }

    /**
     * <b>获取基础的sql函数</b>
     * <p>
     * 不包含排序
     * </p>
     *
     * @return
     */
    private String getBaseSql() {
        String sql = refSql;
        if (!StringUtil.emptyStr(whereSql))
            sql += " where " + whereSql;
        if (StringUtil.emptyStrs(refSql, whereSql)) {
            sql = "";
        }
        return sql;
    }

    /**
     * <b>获取完整的基础sql</b>
     * <p>
     * 包含排序
     * </p>
     *
     * @return
     */
    private String getFullBaseSql() {
        return getBaseSql() + orderSql;
    }
}
